package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;


public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //编码设定：最前面，请求和响应都需要设定编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //根据url请求参数的key获取value
        String username = request.getParameter("username");
        String psw = request.getParameter("psw");
        String gender = request.getParameter("gender");
        //当请求参数key可能对应多个value时
        String[] interests = request.getParameterValues("interest");
        //获取请求参数中所有的name属性列表，返回的是一个枚举
        Enumeration<String> names = request.getParameterNames();
        //获取请求参数所有的key：value键值对
        Map<String, String[]> map = request.getParameterMap();

        System.out.println("username = " + username);
        System.out.println("psw = " + psw);
        System.out.println("gender = " + gender);
        System.out.println("interests = " + Arrays.toString(interests));
        while (names.hasMoreElements()) {
            System.out.println("names.nextElement() = " + names.nextElement());
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //根据url请求参数的key获取value
        String username = request.getParameter("username");
        String psw = request.getParameter("psw");
        String gender = request.getParameter("gender");
        //当请求参数key可能对应多个value时
        String[] interests = request.getParameterValues("interest");
        //获取请求参数中所有的name属性列表，返回的是一个枚举
        Enumeration<String> names = request.getParameterNames();
        //获取请求参数所有的key：value键值对
        Map<String, String[]> map = request.getParameterMap();

        System.out.println("username = " + username);
        System.out.println("psw = " + psw);
        System.out.println("gender = " + gender);
        System.out.println("interests = " + Arrays.toString(interests));
        while (names.hasMoreElements()) {
            System.out.println("names.nextElement() = " + names.nextElement());
        }

    }
}
